package com.wsd.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsd.bean.Message;
import com.wsd.dao.MessageDaoInterface;

/**
 * A data access object (DAO) providing persistence and search support for
 * Message entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wsd.bean.Message
 * @author MyEclipse Persistence Tools
 */

public class MessageDAO extends HibernateDaoSupport implements MessageDaoInterface {
	private static final Log log = LogFactory.getLog(MessageDAO.class);

	protected void initDao() {
		// do nothing
	}

	public void save(Message transientInstance) {
		log.debug("saving Message instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Message persistentInstance) {
		log.debug("deleting Message instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Message findById(java.lang.Integer id) {
		log.debug("getting Message instance with id: " + id);
		try {
			Message instance = (Message) getHibernateTemplate().get(
					"com.wsd.bean.Message", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Message> findAll() {
		log.debug("finding all Message instances");
		try {
			String queryString = "from Message";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.MessageDaoInterface#findOnePage(int, int)
	 */
	public List<Message> findOnePage(int start, int pagesize) {
		log.debug("finding ong page Message instances");
		try {
			String queryString = "from Message m where message.MId is null or message.MId = 0";
			Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
			query.setFirstResult(start);
			query.setMaxResults(pagesize);
			return query.list();
		} catch (RuntimeException re) {
			log.error("find ong page failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.MessageDaoInterface#getTotallines()
	 */
	public List getTotallines() {
		log.debug("finding Message totallines");
		try {
			String queryString = "select count(*) from Message where message.MId is null or message.MId = 0";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find Message totallines failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.MessageDaoInterface#findAll(int, int)
	 */
	public List<Message> findAll(int start, int limit) {
		String sql = "from Message where replay = false order by MId desc";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.MessageDaoInterface#findAllCount()
	 */
	public int findAllCount() {
		String sql = "select count(*) from Message where replay = false";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.MessageDaoInterface#deleteById(int)
	 */
	public void deleteById(int id) {
		String sql = "delete Message where MId = :id";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setInteger("id", id);
		query.executeUpdate();
	}
}